package com.luchao;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import java.util.Properties;
import java.util.Random;

/**
 * 订阅地址生成
 *
 * @author LuChao
 * @since 1.0.0
 */
public class GenerateUtils {


    public static void Generate() throws Exception {
        String[] urls = getAddressUrls();
        String allstr = "";
        String alldestPath = "C:\\qcode\\";
        for (int i = 0; i < urls.length; i++) {
            String destPath = "C:\\qcode\\" + i + "\\";
            String urlstr = urls[i];
            String rawHTML = null;
            try {
                rawHTML = getHTMLContent(urlstr);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (null == rawHTML || rawHTML.indexOf("</") > 0 || rawHTML.contains("Not Found")  || rawHTML.contains("404")) {
                System.out.println("该站异常：" + urlstr);
                continue;
            }
//            System.out.println(rawHTML);
            String ssr_str ="";
            try{
                ssr_str = new String(Base64.getDecoder().decode(rawHTML));
            }catch (Exception e){
                System.out.println("该站异常：" + urlstr);
                continue;
            }
            String[] res = ssr_str.split("\n");
//            System.out.println(res);
            for (int j = 0; j < res.length; j++) {
                String s = res[j];
                if (s.length() < 10 || s.startsWith(":")) {
                    continue;
                }
//                System.out.println(s);
                allstr += s;
                //生成二维码
                QRCodeUtil.encode(s, null, new Random().nextInt(1000000000) + "", destPath, true);
            }
            System.out.println("完成：" + urlstr);
            //导出txt
            OutputStream fos = new FileOutputStream(destPath + "ssr.txt");
            fos.write(ssr_str.getBytes("UTF-8"));    //将字符串变成字节byte数组，使用UTF-8编码
            fos.close();
        }
        //导出所有txt
        OutputStream fos = new FileOutputStream(alldestPath + "allssr.txt");
        fos.write(allstr.getBytes("UTF-8"));    //将字符串变成字节byte数组，使用UTF-8编码
        fos.close();

    }

    private static String[] getAddressUrls() {
        Properties properties = null;
        String addressurls = "";
        try {
            properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
            addressurls = properties.getProperty("ssr.addressurl");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return addressurls.split(",");
    }

    //根据url地址获取对应页面的HTML内容，我们将上一节中的内容打包成了一个方法，方便调用
    private static String getHTMLContent(String url) throws IOException {

        //建立一个新的请求客户端
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //使用HttpGet方式请求网址
        HttpGet httpGet = new HttpGet(url);
        //获取网址的返回结果
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //获取返回结果中的实体
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity);
        //关闭HttpEntity流
        EntityUtils.consume(entity);
        return content;

    }
}

